package com.tjp.eight.ui.fragment.mywallet.zfb;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.tjp.eight.R;
import com.tjp.eight.ui.fragment.BaseFragment;
import com.tjp.eight.ui.fragment.mywallet.wx.TSResultModel;
import com.tjp.eight.ui.fragment.mywallet.wx.WXPModel;
import com.tjp.eight.utils.BitmapUtils;
import com.tjp.eight.utils.IsSuccessUtils;
import com.tjp.eight.utils.SharePreferencesUtils;
import com.tjp.eight.utils.StringUtils;
import com.tjp.eight.utils.ToatUtils;

/**
 * Created by Administrator on 2017/3/28.
 */
public class FragmentZhiFuB extends BaseFragment<ZFBPresenter> implements ZFBView {
    EditText ed_account;
    EditText ed_name;
    EditText ed_money;
    TextView tv_wallet;
    TextView tv_can_wallet;
    TextView tv_wallet_all;
    LinearLayout ll_result;
    LinearLayout ll_container;
    ImageView iv_result;
    TextView tv_result;
    private String money;

    @Override
    protected ZFBPresenter createPresenter() {
        return new ZFBPresenter(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = super.onCreateView(inflater, container, savedInstanceState);
        if (contentView == null) {
            contentView = inflater.inflate(R.layout.fragment_zfb, container, false);
        }
        money = getActivity().getIntent().getStringExtra("money");
        money = Integer.parseInt(money) / 100.00f + "";
        initViews();
        return contentView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        presenter.loaddata(getLPJson());
    }

    private String getLPJson() {
        LPModel model = new LPModel();
        model.uid = SharePreferencesUtils.getUid();
        model.loginkey = SharePreferencesUtils.getLoginKey();
        model.payway = "2";
        return new Gson().toJson(model);
    }

    void initViews() {
        ll_container = (LinearLayout) contentView.findViewById(R.id.ll_container);
        ll_result = (LinearLayout) contentView.findViewById(R.id.ll_result);
        tv_result = (TextView) contentView.findViewById(R.id.tv_result);
        iv_result = (ImageView) contentView.findViewById(R.id.iv_result);
        ed_account = (EditText) contentView.findViewById(R.id.ed_account);
        ed_money = (EditText) contentView.findViewById(R.id.ed_money);
        tv_wallet = (TextView) contentView.findViewById(R.id.tv_wallet);
        ed_name = (EditText) contentView.findViewById(R.id.ed_name);
        tv_can_wallet = (TextView) contentView.findViewById(R.id.tv_can_wallet);
        tv_wallet_all = (TextView) contentView.findViewById(R.id.tv_wallet_all);
        tv_wallet_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ed_money.setText(money);
            }
        });
        tv_can_wallet.setText("可提现金额" + money + "元");

        final Double moenyd = Double.parseDouble(money);
        ed_money.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String temps = s.toString();
                if (!StringUtils.isNullOrEmpty(temps)) {
                    Double temp = Double.parseDouble(temps);
                    if (temp > moenyd) {
                        ed_money.setText(moenyd + "");
                    }
                    if (temp > 0 && !StringUtils.isNullOrEmpty(ed_account.getText().toString())
                            && !StringUtils.isNullOrEmpty(ed_name.getText().toString())) {
                        tv_wallet.setEnabled(true);
                    } else {
                        tv_wallet.setEnabled(false);
                    }
                } else {
                    tv_wallet.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                String temp = s.toString();
                int posDot = temp.indexOf(".");
                if (posDot > 0) {
                    if (temp.length() - posDot - 1 > 2) {
                        s.delete(posDot + 3, posDot + 4);
                    }
                }
            }
        });

        ed_account.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!StringUtils.isNullOrEmpty(ed_money.getText().toString()) && !StringUtils.isNullOrEmpty(ed_account.getText().toString())
                        && !StringUtils.isNullOrEmpty(ed_name.getText().toString())) {
                    tv_wallet.setEnabled(true);
                } else {
                    tv_wallet.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        ed_name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!StringUtils.isNullOrEmpty(ed_money.getText().toString()) && !StringUtils.isNullOrEmpty(ed_account.getText().toString())
                        && !StringUtils.isNullOrEmpty(ed_name.getText().toString())) {
                    tv_wallet.setEnabled(true);
                } else {
                    tv_wallet.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        tv_wallet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.toZFB(getPJson());
            }
        });

    }

    private String getPJson() {
        WXPModel model = new WXPModel();
        model.uid = SharePreferencesUtils.getUid();
        model.loginkey = SharePreferencesUtils.getLoginKey();
        model.money = ed_money.getText().toString();
        model.payway = "2";
        model.account = ed_account.getText().toString();
        model.name = ed_name.getText().toString();
        return new Gson().toJson(model);
    }

    @Override
    public void tSuccess(TSResultModel model) {
        if (model != null) {
            ll_container.setVisibility(View.GONE);
            ll_result.setVisibility(View.VISIBLE);
            if (IsSuccessUtils.isSuccess(model.result)) {

            } else {
                tv_result.setText("提现申请失败！" + model.msg);
                iv_result.setImageBitmap(BitmapUtils.getBitmap(getContext(), R.drawable.wallet_error));
            }
        }
    }

    @Override
    public void fail(String msg) {

    }

    @Override
    public void loadSuccess(TZFBRModel model) {
        if (model != null) {
            if (IsSuccessUtils.isSuccess(model.result)) {
                if (model.data != null) {
                    if (!StringUtils.isNullOrEmpty(model.data.tixian_account)) {
                        ed_account.setText(model.data.tixian_account);
                    }
                    if (!StringUtils.isNullOrEmpty(model.data.tixian_name)) {
                        ed_name.setText(model.data.tixian_name);
                    }
                    if (!StringUtils.isNullOrEmpty(model.money_rest)) {
                        try {
                            money = Double.parseDouble(model.money_rest) / 100.00f + "";
                            tv_can_wallet.setText("可提现金额" + money + "元");
                        }catch (NumberFormatException e){

                        }
                    }
                }
            } else {
                ToatUtils.toastSuccess(getContext(), "" + model.msg);
            }
        }
    }
}
